﻿using Messenger.Models;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Messenger.ViewModels;
using CommunityToolkit.Mvvm.ComponentModel;
using Messenger.Views;
using Messenger.WinDialogs;
using Microsoft.Xaml.Behaviors;
using Messenger.Utils;

namespace Messenger
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly LogService logService = new LogService();
        public MainWindow()
        {
            InitializeComponent();
        }
        
        private async void Login_Click(object sender, RoutedEventArgs e)
        {
            string username = "alice";
            string password = "123456";

            await logService.LoginAsync(username, password);
        }

        private void OpenNewWindow_Click(object sender, RoutedEventArgs e)
        {
            var view = new StudentView();

            ShowWinDialogContext showWinDialogContext = new ShowWinDialogContext()
            {
                WinStyle = this.FindResource("StudentViewWinStyle") as Style,
                Title = "StudentView",
                Owner = Window.GetWindow(this),
                View = view,
            };
            var result = WinDialog.ShowDialog(showWinDialogContext);
        }

        private void OpenCBWindow_Click(object sender, RoutedEventArgs e)
        {
            var view = new ComboBoxView();

            ShowWinDialogContext showWinDialogContext = new ShowWinDialogContext()
            {
                WinStyle = this.FindResource("ComboBoxViewWinStyle") as Style,
                Title = "ComboBoxView",
                Owner = Window.GetWindow(this),
                View = view,
            };

            var result = WinDialog.ShowDialog(showWinDialogContext);
            if (result.Result == true)
            {
                var viewModel = view.ViewModel;
                var appConfig = AppConfiguration.Instance;
                appConfig.SkipPowerBoardCheck = viewModel.SkipPowerBoardCheck;
                appConfig.SkipBibSlotCheck = viewModel.SkipBibSlotCheck;
                appConfig.RecordTcp = viewModel.RecordTcp;
                appConfig.WriteConfigFile();


            }
        }

        private void OpenBehaviorsAndTriggerButton_Click(object sender, RoutedEventArgs e)
        {
            var view = new BehaviorsAndTriggersView();

            ShowWinDialogContext showWinDialogContext = new ShowWinDialogContext()
            {
                WinStyle = this.FindResource("BehaviorAndTrigger") as Style,
                Title = "行为与触发器",
                Owner = Window.GetWindow(this),
                View = view,
            };
            var result = WinDialog.ShowDialog(showWinDialogContext);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //var config = AppConfiguration.Instance;

        }
    }
}